package com.forcht.client.view;

import com.forcht.client.Client;
import com.forcht.common.domain.Message;
import com.forcht.common.domain.MessageHeader;
import com.forcht.common.enumeration.MessageType;
import com.forcht.common.util.ProtostuffUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Date;

/**
 * Created by forcht on 2018/4/12.
 */
public class ChatView extends JFrame implements ActionListener, WindowListener {

    public static final String WRAP = "\r\n";
    private JTextArea contentArea;
    private JTextField contentField;
    private JButton sendButton;
    private JPanel panel;
    private JScrollPane scrollPane;
    private Client client;
    private String chatUsername;

    public ChatView(Client client, String chatUsername) {
        this.client = client;
        this.chatUsername = chatUsername;
        contentArea = new JTextArea();
        contentArea.setForeground(Color.red);
        contentField = new JTextField(15);
        sendButton = new JButton("发送");
        sendButton.addActionListener(this);
        panel = new JPanel();
        panel.add(contentField);
        panel.add(sendButton);
        scrollPane = new JScrollPane(contentArea);

        this.add(scrollPane, "Center");
        this.add(panel, "South");

        this.setTitle("正在" + chatUsername + "与聊天");
        this.setSize(350, 300);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sendButton) {
            String content = contentField.getText().trim();
            contentField.setText("");
            if (content.length() > 0) {
                SocketChannel socketChannel = client.getUser().getSocketChannel();
                String sender = client.getUser().getUsername();
                contentArea.append(sender + ":" + content + WRAP);
                Message message = Message.builder().header(MessageHeader.builder().sender(sender)
                        .receiver(chatUsername).timestamp(new Date())
                        .messageType(MessageType.CHAT_ONE).build()).body(content.getBytes()).build();
                try {
                    socketChannel.write(ByteBuffer.wrap(ProtostuffUtil.serialize(message)));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    /**
     * 更新聊天记录
     *
     * @param content
     */
    public void updateContentArea(String content) {
        contentArea.append(chatUsername + ":" + content + WRAP);
    }

    public static void main(String[] args) {
        new ChatView(null, null);
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        client.removeChatView(chatUsername);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
